package com.example.teachdemo4;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;

public class MediaPlayerActivity extends AppCompatActivity {

    MediaPlayer mediaPlayer;
    boolean isReady = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_player);
        //mediaPlayer = MediaPlayer.create(this,Uri.parse("android.resource://com.example.teachdemo4/"
        // + R.raw.qhc));
        mediaPlayer = MediaPlayer.create(this,R.raw.abc);
        if (mediaPlayer == null) return;
        try {
            mediaPlayer.stop();
            mediaPlayer.prepare();
            isReady = true;

        }catch (Exception e){
            isReady = false;
        }

        if(isReady){
            if(!mediaPlayer.isPlaying()){
                mediaPlayer.start();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mediaPlayer != null && mediaPlayer.isPlaying()){
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }
}
